import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AppService } from './../../app/app.serve';

@Component({
  selector: 'page-tab-dynamic',
  templateUrl: 'tab-dynamic.html'
})
export class TabDynamicPage {
  select = 0;
  tabs = [];
  newDynamicList = [];
  newPriseList = [];
  page = 1;
  constructor(public navCtrl: NavController, public appService: AppService) {
    this.tabs = [
      { key: '最新参与记录', id: 0, isSelect: true },
      { key: '最新获奖记录', id: 1, isSelect: false },
      { key: '揭晓记录', id: 2, isSelect: false }
    ]
  }
  ngOnInit() {
    this.getRecord(1)
  }
  checktab(index: number) {
    console.log("index" + index);
    this.tabs.forEach(tab => { tab.isSelect = false });
    this.tabs[index].isSelect = true;
    this.select = index;
    this.page = 1;
    this.newPriseList = [];
    this.newDynamicList = [];
    this.getRecord(index + 1)
  }
  doRefresh(e) {
    console.log('刷新')
    this.newPriseList = [];
    this.newDynamicList = [];
    this.page = 1;
    this.getRecord(this.select + 1, false);
    setTimeout(() => {
      e.complete();
    }, 1500);
  }
  doInfinite(e) {
    console.log('加载更多')
    this.page++;
    this.getRecord(this.select + 1, false)
    setTimeout(() => {
      e.complete();
    }, 1500);
  }
  getRecord(askType, showLoading = true) {
    let urlJoin = '/apinewest/newest_join'
    let urlBuy = '/apinewest/newest_buying'
    let urlPrize = '/apinewest/newest_prize'
    let url = '';
    if (askType == 1) {
      url = urlJoin
    } else if (askType == 2) {
      url = urlBuy
    } else if (askType == 3) {
      url = urlPrize
    }
    this.appService.post(url, { newest: 'newest', page: this.page }, res => {
      console.log(res)
      if (askType == 3) {
        res.data.forEach(item => {
          item.awardnum = item.awardnum.split(",");
          item.isOdd = item.num5 % 2 == 0;
        });
        this.newPriseList.push(...res.data)
      } else {
        this.newDynamicList.push(...res.data)
      }
    }, showLoading)
  }


}
